package cn.fhkj.service.impl;

import cn.fhkj.config.ProjectProperties;
import cn.fhkj.service.IService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
@Slf4j
public abstract class DefaultService<T> implements IService<T> {
    public static final int PAGESIZE = 10;

    @Autowired
    protected HttpServletRequest request;
    @Autowired private ProjectProperties projectProperties;

    private static final Logger logger = LoggerFactory.getLogger(DefaultService.class);

    @Override
    public T getById(int id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int deleteById(int id) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int delete(T entity) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int update(T entity) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int insert(T entity) {
        // TODO Auto-generated method stub
        return 0;
    }

    /**
     * 如果分页信息不为null，则调用mybatis的分页插件进行分页。
     *
     * @param currentPage 从1开始
     * @param pageSize    不指定，或指定为0时，则忽略currentPage，返回所有记录。
     */
    protected void paging(Integer currentPage, Integer pageSize) {
        if (currentPage != null && pageSize != null) {
            int start = 1;
            int size = 0;
            if (currentPage >= 1) {
                start = currentPage;
            }
            if (pageSize != null || pageSize > 0) {
                size = pageSize;
            }
            PageHelper.startPage(start, size);
        }
    }


    /**
     * 如果分页信息不为null，则调用mybatis的分页插件进行分页。
     *
     * @param currentPage 从1开始
     * @param pageSize    不指定，或指定为0时，则忽略currentPage，返回所有记录。
     */
    protected Page<Object> pagingReasonable(Integer currentPage, Integer pageSize) {
        if(currentPage == null || pageSize == null){
            return null;
        }
        int start = 1;
        int size = PAGESIZE;
        if (currentPage != null && currentPage >= 1) {
            start = currentPage;
        }

        if (pageSize != null && pageSize > 0) {
            size = pageSize;
        }
        Page<Object> paging = PageHelper.startPage(start, size);
        paging.setReasonable(false);
        return paging;

    }

    /**
     * 处理资源，能够通过url访问。 本项目的web和manage，图片保存在指定文件夹内，web通过指定的配置urlpath，来获得访问url。 如果资源已经是一个完整url，那么不进行处理。
     *
     * @param resourcePath
     * @return
     */
    protected final String handleResToUrl(String resourcePath) {
        if (resourcePath != null
                && !resourcePath.trim().equals("")
                && !resourcePath.startsWith("http")) {
            String resUrl = projectProperties.getUrlpath() + resourcePath;
            log.debug("原资源路径[{}]更改后的存储url为[{}]", resourcePath, resUrl);
            return resUrl;
        } else {
            return resourcePath;
        }
    }
    protected static String htmlspecialchars(String str) {
        str = str.replaceAll("&amp;", "&amp;");
        str = str.replaceAll("& lt;", "<");
        str = str.replaceAll("& gt;", ">");
        str = str.replaceAll("&quot;", "\"");
        str = str.replaceAll("&nbsp;", " ");
        str = str.replaceAll("& #40;", "(");
        str = str.replaceAll("& #41;", ")");
        return str;
    }

}
